/*
 * 创建日期 2009-12-14
 *
 * 成都天和软件公司
 * 电话：028-85425861
 * 传真：028-85425861-8008
 * 邮编：610041 
 * 版权所有
 */
package  org.fantasy.common.grid.head;

import  org.fantasy.common.grid.bean.Column;
import  org.fantasy.common.util.StringUtil;
import  org.fantasy.common.util.XMLUtil;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.Element;

/**
 *  动态表头配置
 * @author: 王文成
 * @version: 1.0
 * @since 2009-12-14
 */
public abstract class DynaTableHead extends TableConfigSupport{
    
    private static final Log log = LogFactory.getLog(DynaTableHead.class);
    
    @Override
    protected Element getTableConfig() throws Exception {
        List<Column> cols = getColumns();
        StringBuffer html = new StringBuffer(512);
        String name = DynaTableHead.class.getName();
        html.append("<table name=\"" + name+ "\"  sumTotal=\"" + isSumTotal() +"\" subTotal=\"" + isSubTotal() +"\"> \n");
        for( int i = 0 ; i < cols.size(); i++ ){
            Column col = cols.get(i);
            String dataType = StringUtil.tryThese(new String[]{col.getDataType(),"string"});
            html.append("    <column> \n");
            html.append(        addAttribute("fieldName" , col.getFieldName() ));
            html.append(        addAttribute("fieldLable" , col.getFieldLabel() ));
            html.append(        addAttribute("visible" , col.getVisible() ));
            html.append(        addAttribute("dataType" , dataType ));
            html.append(        addAttribute("detail" , col.getDetail() ));
            html.append(        addAttribute("click" , col.getClick() ));
            html.append(        addAttribute("export" , col.isExport() ));
            html.append(        addAttribute("align" , col.getAlign() ));
            html.append(        addAttribute("isStat" , col.getIsStat() ));
            html.append(        addAttribute("isSum" , col.getIsSum() ));
            html.append(        addAttribute("desc" , col.getDesc() ));
            html.append(        addAttribute("comment" , col.getComment() ));
            html.append(        addAttribute("rowspan" , col.getRowspan() ));
            html.append(        addAttribute("expression" , col.getExpression() ));
            html.append(        addAttribute("scale" , col.getScale() ));
            html.append("    </column> \n");
        }
        html.append("</table> \n");
        log.info(html);
        String xml = "<?xml version=\"1.0\" encoding=\"gb2312\"?>\n" + html;
        Document document = XMLUtil.fromXML(xml, "GBK");
        List tables = document.selectNodes("/table[@name]");
        return (Element) tables.get(0);
    }
    
    private String addAttribute(String param , Object value ){
        StringBuffer xml = new StringBuffer();
        if( StringUtil.isValid(value) ){
            xml.append("        <attribute param=\"" + param + "\">" + value + "</attribute> \n");
        }
        return xml.toString();
    }

    /**
     * 取得表头配置的所有列
     * @return
     * @throws Exception
     */
    public abstract List<Column> getColumns() throws Exception;
    
    /**
     * 是否合计
     * @return
     * @throws Exception
     */
    protected boolean isSumTotal() throws Exception{
        return false;
    }
    
    /**
     * 是否小记
     * @return
     * @throws Exception
     */
    protected boolean isSubTotal() throws Exception{
        return false;
    }
    
    /**
     * 行高
     * @return
     * @throws Exception
     */
    protected int getRowHeight() throws Exception{
        return 20;
    }
}
